<?php
class SideboxesController extends AppController {

    public $helpers = array('Pagination');

    public function admin_index() {
        $this->Sidebox->recursive = 0;
        $this->paginate = array('order'=>'priority desc');
        $this->set('sideboxes', $this->paginate());
    }

    public function admin_view($id = null) {
        if (!$this->Sidebox->findById($id)) {
            $this->Session->setFlash(__('Invalid sidebox',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        $this->set('sidebox', $this->Sidebox->read(null, $id));
    }

    public function admin_add() {
        if (!empty($this->data)) {
            $this->Sidebox->create();
            if ($this->Sidebox->save($this->data)) {
                $this->Session->setFlash(__('The Sidebox has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Sidebox could not be saved. Please, try again.',true));
            }
        }
    }

    public function admin_edit($id = null) {
        if (!$this->Sidebox->findById($id)) {
            $this->Session->setFlash(__('Invalid sidebox',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if (!empty($this->data)) {
            if ($this->Sidebox->save($this->data)) {
                $this->Session->setFlash(__('The sidebox has been saved',true));
                $this->redirect(array('action'=>'index'), null, true);
            } else {
                $this->Session->setFlash(__('The Sidebox could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->Sidebox->read(null, $id);
        }
    }

    public function admin_delete($id = null) {
        if (!$this->Sidebox->findById($id)) {
            $this->Session->setFlash(__('Invalid sidebox',true));
        }
        if ($this->Sidebox->del($id)) {
            $this->Session->setFlash(__('Sidebox deleted',true));
        } else {
            $this->Session->setFlash(__('The Sidebox could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function admin_delete_sideboxes() {
        if($this->Sidebox->deleteAll(array('Sidebox.id'=>$this->data['SideboxesDelete']))) {
            $this->Session->setFlash(__('Sideboxes deleted',true));
        } else {
            $this->Session->setFlash(__('The Sideboxes could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'), null, true);
    }

    public function right() {
        return $this->Sidebox->right_sideboxes();
    }
}
?>